package com.habdqn.easybuy.web.front;

import com.habdqn.easybuy.dao.NewsDao;
import com.habdqn.easybuy.entity.News;
import com.habdqn.easybuy.service.NewsService;
import com.habdqn.easybuy.service.impl.NewsServiceImpl;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/12/2
 */
@WebServlet(name = "NewsServlet",urlPatterns = "/news")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        NewsService newsService = new NewsServiceImpl();
        String strId = request.getParameter("id");
        if (StringUtils.isNotBlank(strId)){
            News news = newsService.queryById(Integer.parseInt(strId));
            request.setAttribute("news",news);
        }
        List<News> newsList = newsService.queryAll();
        request.setAttribute("newsList",newsList);
        request.getRequestDispatcher("/WEB-INF/front/news-view.jsp").forward(request,response);
    }
}
